package com.xiaoliang.servlet;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author XIAOLIANG
 * @ClassName VerificationCode.java
 * @Description TODO
 * @createTime 2021年02月22日 19:35:00
 */
@WebServlet("/VerificationCode")
public class VerificationCode extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 50);  //获取验证码图片

		String code = lineCaptcha.getCode();  //获取验证码的值

		HttpSession session = req.getSession();

		session.setAttribute("verificationCode",code);  //将验证码的值 存入session 如果已经存在就更新
		System.out.println(code);
		lineCaptcha.write(resp.getOutputStream());  //输出到浏览器页面

	}
}
